package com.ken.mqbase.confirm;

import com.ken.constant.RabbitConstant;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;

/**
 * 响应式  生产者
 * Created by renchongyu on 2018/11/15.
 */
public class ProductConfirm {

    public static void main(String[] args) throws Exception{

        //1.创建一个ConnectionFactory
        ConnectionFactory conn = new ConnectionFactory();
        conn.setHost(RabbitConstant.RABBITMQ_IP);
        conn.setPort(RabbitConstant.RABBITMQ_PORT);
        conn.setUsername(RabbitConstant.RABBITMQ_USER_NAME);
        conn.setPassword(RabbitConstant.RABBITMQ_USER_PASSWORD);
        conn.setVirtualHost(RabbitConstant.RABBITMQ_VIRTUAL_HOST);

        //2.通过连接工厂连接
        Connection connection = conn.newConnection();

        //3.通过连接创建一个channel
        Channel channel = connection.createChannel();

        //4.指定我们的消息投递模式：消息确认模式
        channel.confirmSelect();

        String exchangeName = "test_confirm_exchange";
        String routingKey = "confirm.save";

        //5.发送一条消息
        String msg = "Hello RabbitMQ! from Ken's to confirm message!";

        channel.basicPublish(exchangeName,routingKey,null,msg.getBytes());

        //6.添加一个确认监听
        channel.addConfirmListener(new ConfirmListener() {
            //处理成功
            @Override
            public void handleAck(long l, boolean b) throws IOException {
                System.out.println("处理成功");
            }
            //处理失败
            @Override
            public void handleNack(long l, boolean b) throws IOException {
                System.out.println("处理失败");
            }
        });
    }
}
